from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

import app, os, sys
from app import config

def run(mode):  
    uri = str(os.environ.get("PATH_INFO"))
    uri_parts = uri.split("/")
    
    if True:
        type = "app.api"
            
        if "api" == uri_parts[1] :
            action = eval(type + "." + uri_parts[2] + "." + uri_parts[3] + ".Action")
        else:
            type = "app.web"
            
            try:
                
                config.module = uri_parts[1]
                config.action = uri_parts[2]
                
                action = eval(type + "." + uri_parts[1] + "." + uri_parts[2] + ".Action")
                
            except IndexError:
                
                sys.exit()
            
        if not issubclass(action, app.abstract.Action): sys.exit()
        
        map = [(uri, action)]

    #print map

    application = webapp.WSGIApplication(map, debug = not app.config.PRODUCTION_MODE)
    run_wsgi_app(application)

if __name__ == "__main__":
    run(app.config.PRODUCTION_MODE)